package com.boot_reggie.Config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.boot_reggie.filter.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
@Component
public class MyMetaObjectthandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        System.out.println("userId===============" + BaseContext.getThreadLocal1());
        System.out.println("userId===============" + BaseContext.getCurrentID());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        if(BaseContext.getCurrentID() == null){
            metaObject.setValue("createUser", BaseContext.getThreadLocal1());
        }else {
            metaObject.setValue("createUser", BaseContext.getCurrentID());
        }
        if(BaseContext.getCurrentID() == null){
            metaObject.setValue("updateUser", BaseContext.getThreadLocal1());
        }else {
            metaObject.setValue("updateUser", BaseContext.getCurrentID());
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentID());
    }
}
